# -*- coding: utf-8 -*-
"""
@Project: base_python
@File: btc_close_2017.py
@Author: PC
@Date: 2025/03/21
@Description: 
"""
import json
import pygal 
from itertools import groupby

# 将数据加载到一个列表中
filename = 'btc_close_2017.json'
with open(filename) as f:
    btc_data = json.load(f)
# 打印每一天的信息
dates = []
months = []
weeks = []
weekdays = []
close = []
for btc_dict in btc_data:
    dates.append(btc_dict['date'])
    months.append(int(btc_dict['month']))
    weeks.append(int(btc_dict['week']))
    weekdays.append(btc_dict['weekday'])
    close.append(int(float(btc_dict['close'])))

# 定义绘制折线图的函数
def draw_line(x_data,y_data,title,y_legend): 
    """
    绘制折线图
    :param x_data: X轴数据
    :param y_data: Y轴数据
    :param title: 图表标题
    :param y_legend: Y轴图例
    :return: 折线图对象
    """
    xy_map = []
    for x,y in groupby(sorted(zip(x_data,y_data)),key=lambda _:_[0]):
        y_list = [v for _,v in y]
        xy_map.append([x,sum(y_list) / len(y_list)]) 
    x_unique,y_mean = [*zip(*xy_map)]
    line_chart = pygal.Line()
    line_chart.title = title
    line_chart.x_labels = x_unique
    line_chart.add(y_legend,y_mean)
    line_chart.render_to_file(title+'.svg')
    return line_chart

# 找到12月的索引位置
idx_month = dates.index('2017-12-01')
# 绘制月日均值折线图
line_chart_month = draw_line(months[:idx_month],close[:idx_month],'收盘价月日均值(¥)',
'月日均值')

# 找到12月11日的索引位置
idx_week = dates.index('2017-12-11')
# 绘制周日均值折线图
line_chart_week = draw_line(weeks[1:idx_week],close[1:idx_week],'收盘价周日均值(¥)','周日均值')

# 找到12月11日的索引位置
idx_week = dates.index('2017-12-11')
# 将星期转换为整数
wd = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
weekdays_int = [wd.index(w)+1 for w in weekdays[1:idx_week]]
# 绘制星期均值折线图
line_chart_weekday = draw_line(weekdays_int,close[1:idx_week],'收盘价星期均值(¥)',
   '星期均值')
# 设置X轴标签
line_chart_weekday.x_labels = ['周一','周二','周三','周四','周五','周六','周日']
# 渲染并保存折线图
line_chart_weekday.render_to_file('收盘价星期均值(¥).svg')


with open('收盘价Dashboard.html', 'w', encoding='utf8') as html_file:
    html_file.write('<html><head><title>收盘价Dashboard</title><meta charset="utf-8"></head><body>\n')
    for svg in [
            '收盘价折线图(¥).svg','收盘价对数变换折线图(¥).svg','收盘价月日均值(¥).svg',
            '收盘价周日均值(¥).svg','收盘价星期均值(¥).svg'
    ]:
        html_file.write('   <object type="image/svg+xml" data="{0}" height=500></object>\n'.format(svg)) 
    html_file.write('</body></html>')